package com.wechat.common.interceptor.config;

import com.wechat.common.interceptor.core.UserInfoInterceptor;
import com.wechat.common.security.token.TokenUtil;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class InterceptorConfig implements WebMvcConfigurer {

    private final TokenUtil tokenUtil;

    public InterceptorConfig(TokenUtil tokenUtil) {
        super();
        this.tokenUtil = tokenUtil;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInfoInterceptor(tokenUtil))
                // 拦截所有请求
                .addPathPatterns("/**")
                // 排除不需要登录验证的路径
                .excludePathPatterns(
                        // 登录相关接口
                        "/api/auth/login",
                        "/api/auth/register",
                        "/api/auth/refresh-token",
                        "/api/auth/send-verification-code",
                        // 公共资源
                        "/static/**",
                        "/css/**",
                        "/js/**",
                        "/images/**",
                        "/favicon.ico",
                        // 健康检查
                        "/actuator/**",
                        "/health",
                        // API文档相关
                        "/swagger-ui/**",
                        "/v3/api-docs/**",
                        "/swagger-resources/**",
                        "/webjars/**",
                        // 错误页面
                        "/error"
                );
    }
}
